/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db;

import com.mysql.jdbc.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

/**
 *
 * @author xDrake
 */
public class MySQL {

    private Statement stmt = null;
    private Connection con = null;
    private String driver = "";
    private String host = "";
    private String database = ""; // "coderz_req";
    private String user = ""; //coderz_req";
    private String pass = ""; //flatron";

    public MySQL(String host, String database, String user, String pass) {

        this.driver = "com.mysql.jdbc.Driver";
        this.host = host;
        this.database = database;
        this.user = user;
        this.pass = pass;
    }

    public boolean Connect() {
        try {
            Class.forName(this.driver);
            if (this.con != null) {
                this.con.close();
            }
            this.con = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/" + database, user, pass);

            if (this.stmt != null) {
                this.stmt.close();
            }
            this.stmt = (Statement) this.con.createStatement();

            if (this.con != null && this.stmt != null) {
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            return false;
        }
    }

    public boolean Close() {
        try {
            this.con.close();
            this.stmt.close();
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    public ResultSet querySelect(String sql) {
        try {
            ResultSet res = stmt.executeQuery(sql);
            return res;

        } catch (Exception ex) {
            return null;
        }

    }

    public int queryUpdate(String sql) {
        try {
            return stmt.executeUpdate(sql);
        } catch (Exception ex) {
            return -1;
        }
    }
}
